<?php
namespace Game\Factory;

/**
 * Description of ActionFactory
 *
 * @author pahhan
 */
class ActionFactory
{
    /**
     *
     * @var ActionFactory
     */
    protected static $_isntance;

    /**
     *
     * @var \Game\Action\Character\PunchAction
     */
    protected $_punch;

    /**
     *
     * @var array of actions
     */
    protected $_actions = array();

    /**
     *
     * @return ActionFactory
     */
    public static function getInstance()
    {
        if( is_null(self::$_isntance) )
            self::$_isntance = new ActionFactory();

        return self::$_isntance;
    }

    public function __construct()
    {
        $this->_punch = new \Game\Action\Character\PunchAction();
    }

    /**
     *
     * @return \Game\Action\Character\PunchAction
     */
    public function createPunch()
    {
        $punch = clone $this->_punch;
        return $punch;
    }

    public function hasAction($name)
    {
        return array_key_exists($name, $this->_actions);
    }

    /**
     *
     * @param string $name
     * @return \Game\Action\AbstractAction
     * @throws \InvalidArgumentException
     */
    public function createAction($name)
    {
        if( !$this->hasAction($name) )
        {
            switch ($name) {
                case 'punch':
                    $action = new \Game\Action\Character\PunchAction();
                    break;

                case 'walk':
                    $action = new \Game\Action\Character\WalkAction();
                    break;

                default:
                    throw new \InvalidArgumentException(sprintf('Unknown action "%s"', $name));
                    break;
            }
            $this->_actions[$name] = $action;
        }

        return clone $this->_actions[$name];
    }


}

